/*
 * PacketFileMediaSink.h
 *
 *  Created on: 2016年10月10日
 *      Author: zhengboyuan
 */

#ifndef PACKETFILEMEDIASINK_H_
#define PACKETFILEMEDIASINK_H_

#include "FileMediaSink.h"
#include "TCriticalSection.h"
#include "TThread.h"
#include "PacketQueue.h"
#include "TimeRectifier.h"


namespace av
{

class PacketFileMediaSink: public FileMediaSink , comn::Thread
{
public:
	PacketFileMediaSink();
	virtual ~PacketFileMediaSink();

	virtual bool open(const char* filename);
	virtual void close();
	virtual bool isOpen();

	virtual void onMediaFormat(const MediaFormat& fmt);

	virtual void onMediaPacket(AVPacketPtr& pkt);

	virtual void onMediaEvent(int event);

	virtual int setFile(const char* filename);

	virtual const char* getFile();

	
	void writePacket(int type, uint8_t* data, int size, int64_t pts, int duration, int flags);

protected:
	virtual int run();
	virtual void doStop();

	void handlePacket(AVPacketPtr& pkt);

protected:
	bool openFile(const char* filename, const MediaFormat& fmt);

	void writeVideo(AVPacketPtr& inPkt);
	void writeAudio(AVPacketPtr& inPkt);

protected:
	bool openFile();
	void closeFile();
	bool isFileOpen();

protected:
	comn::CriticalSection   m_cs;

	MediaFormat m_fmt;

	std::string m_fileName;
	std::string m_idxName;

	int	m_maxDuration;	/// milliseconds

	bool    m_changeFile;
	PacketQueue m_pktQueue;

	FILE*	m_file;
	FILE*	m_idxFile;


};



} /* namespace av */

#endif /* PACKETFILEMEDIASINK_H_ */
